package com.rcloud.stock.ui.detail.acticle;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rcloud.stock.R;
import com.rcloud.stock.bean.Gg;
import com.rcloud.stock.bean.Zx;
import com.rcloud.stock.ui.detail.DetailViewModel;

public class ArticleFragment extends Fragment {

    private TextView articleTv;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        DetailViewModel model = new ViewModelProvider(requireActivity()).get(DetailViewModel.class);
        //初始化控件
        View view = inflater.inflate(R.layout.fragment_article, container, false);
        articleTv = view.findViewById(R.id.articleTv);

        //接收并请求数据
        Bundle bundle = getArguments();
        if (bundle.getSerializable("zx")!=null){
            Zx zx = (Zx) bundle.getSerializable("zx");
            if (TextUtils.isEmpty(zx.getText()))
                model.fetchZxByUrl(zx.getUrl());
            else
                articleTv.setText(Html.fromHtml(zx.getText()));
        }else{
            Gg gg = (Gg) bundle.getSerializable("gg");
            if (TextUtils.isEmpty(gg.getText()))
                model.fetchZxByUrl(gg.getUrl());
            else
                articleTv.setText(Html.fromHtml(gg.getText()));
        }
        //观察数据
        model.articleLive.observe(getViewLifecycleOwner(),s -> {
            if (s!=null){
                articleTv.setText(Html.fromHtml(s));
            }
        });
        return view;
    }
}